package cn.ym.aircraft;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

public abstract class FlyingObject {
    protected int width;
    protected int height;
    protected int x;
    protected int y;
    protected int speed;
    protected static int LIVE = 0;
    protected static int DEAD = 1;
    protected static int REMOVE = 2;
    protected int state = LIVE;

    FlyingObject(int width, int height) {
        this.width = width;
        this.height = height;
        this.y = -height;
        Random rand = new Random();
        this.x = rand.nextInt(World.WIDTH - this.width);
    }

    FlyingObject(int width, int height, int speed, int x, int y) {
        this.width = width;
        this.height = height;
        Random rand = new Random();
        this.speed = speed;
        this.x = x;
        this.y = y;
    }

    /**
     * 移动
     */
    public abstract void step();

    /**
     * 获取对象图片
     */
    public abstract BufferedImage getImage();


    public void paintImage(Graphics g) {
        g.drawImage(this.getImage(), this.x, this.y, null);
    }

    /**
     * 是否存活
     *
     * @return boolean
     */
    public boolean isLive() {
        return state == LIVE;
    }

    /**
     * 是否死亡
     *
     * @return boolean
     */
    public boolean isDead() {
        return state == DEAD;
    }

    /**
     * 是否被删除
     *
     * @return boolean
     */
    public boolean isRemove() {
        return state == REMOVE;
    }

    /**
     * 判断越界
     *
     * @return boolean
     */
    public boolean isOutOfBound() {
        return this.y > World.HEIGHT;
    }

    /**
     * 对象碰撞算法
     * @param object FlyingObject
     * @return boolean
     */
    public boolean isHit(FlyingObject object) {
        int x1 = this.x - object.width;
        int y1 = this.y - object.height;
        int x2 = this.x + this.width;
        int y2 = this.y + this.height;
        return x1 <= object.x && object.x <= x2
                &&
                y1 <= object.y && object.y <= y2;
    }

    /**
     * 修改对象状态
     */
    public void goDead(){
        state = DEAD;
    }

}
